//电子卡流水详情
const app = getApp()
var server = require('../../utils/server');
var common = require('../../utils/common');
Page({
  data: {
    cardid: "",
    lists: [],
    classHeight: '',
    page: 1,
    isFirstSearch: true,
    searchLoading: false, //"上拉加载"的变量，默认false，隐藏
    searchLoadingComplete: false, //“没有数据”的变量，默认false，隐藏
  },
  onLoad: function (options = {}) {
    this.setData({
      cardid: options.cardid
    })
  },
  onShow: function () {
    this.getLists();
    this.setData({
      classHeight: wx.getSystemInfoSync().windowHeight
    })
  },
  getLists() {
    server.postJSON(common.basePath + 'card/flow/', {
      "city": app.globalData.city,
      "bind_id": this.data.cardid,
      "page": this.data.page,
      "page_size": 30
    }, res => {
      if (res.data.errno == '0') {
        this.setData({
          searchLoading: res.data.data.list.length < 30 ? false : true,
          searchLoadingComplete: res.data.data.list.length < 30 ? true : false,
          lists: res.data.data.list
        })
      } else {
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  toLower() {
    let that = this;
    if (that.data.searchLoading && !that.data.searchLoadingComplete) {
      that.setData({
        page: that.data.page + 1, 
        isFirstSearch: false 
      });
      server.postJSON(common.basePath + 'card/flow/', {
        "city": app.globalData.city,
        "bind_id": this.data.cardid,
        "page": this.data.page,
        "page_size": 30
      }, res => {
        if (res.data.errno == '0') {
          this.setData({
            searchLoading: res.data.data.list.length < 30 ? false : true,
            searchLoadingComplete: res.data.data.list.length < 30 ? true : false
          })
          this.loadMore(res);
        } else {
          common.showErrorToast(res.data.errmsg);
        }
      })
    }
  },
  loadMore(res) {
    if (res.data.data.list.length != 0) {
      let searchList = [];
      this.data.isFirstSearch ? searchList = res.data.data.list : searchList = this.data.lists.concat(res.data.data.list)
      this.setData({
        lists: searchList 
      });
    } else {
      this.setData({
        searchLoadingComplete: true, 
        searchLoading: false 
      });
    }
  },
  goToIndex(){
    wx.switchTab({
      url: '../../pages/index/index',
    })
  }
})